/**
 * Copyright (C) 2010 altuure <altuure [AT] gmail [DOT] com> http://www.altuure.com/projects/yagdao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.altuure.yagdao.model;

import com.altuure.yagdao.YFetch;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "erp_order")
@YFetch("customer")
public class Order {
    @Id
    @GeneratedValue
    public long id;
    @Column
    public String name;

    @Column
    public Date orderDate;

    @Column
    public double totalValue;

    @Column
    public Date deliveryDate;

    @Column
    public int priority;

    @OneToMany(mappedBy = "order")
    public Set<OrderItem> items = new HashSet();

    @ManyToOne(optional = true, fetch = FetchType.LAZY)
    public Customer customer;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getOrderDate() {
        return orderDate;
    }

    public void setOrderDate(Date orderDate) {
        this.orderDate = orderDate;
    }

    public double getTotalValue() {
        return totalValue;
    }

    public void setTotalValue(double totalValue) {
        this.totalValue = totalValue;
    }

    public Date getDeliveryDate() {
        return deliveryDate;
    }

    public void setDeliveryDate(Date deliveryDate) {
        this.deliveryDate = deliveryDate;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public Set<OrderItem> getItems() {
        return items;
    }

    public void setItems(Set<OrderItem> items) {
        this.items = items;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).append("id", id).append("name", name).append("orderDate", orderDate)
                .append("totalValue", totalValue).append("deliveryDate", deliveryDate).append("priority", priority)
                .toString();
    }

    @Override
    public boolean equals(final Object other) {
        if (!(other instanceof Order))
            return false;
        Order castOther = (Order) other;
        return new EqualsBuilder().append(id, castOther.id).append(name, castOther.name)
                .append(orderDate, castOther.orderDate).append(totalValue, castOther.totalValue)
                .append(deliveryDate, castOther.deliveryDate).append(priority, castOther.priority).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(id).append(name).append(orderDate).append(totalValue).append(deliveryDate)
                .append(priority).toHashCode();
    }

}
